package com.uum.blog.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uum.blog.pojo.entity.Category;
import com.uum.blog.pojo.entity.Tag;
import com.uum.blog.pojo.query.CategoryPageQuery;
import com.uum.blog.service.CategoryService;
import com.uum.common.core.annotation.Operation;
import com.uum.common.core.enums.BusinessType;
import com.uum.common.core.result.Result;
import com.uum.web.util.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


@Api(tags = "分类接口")
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/category")
public class CategoryController {
    private final CategoryService categoryService;

    @Operation(description = "查询分类列表",businessType = BusinessType.QUERY,isSaveResponseData = false)
    @ApiOperation(value = "分类列表分页")
    @GetMapping("page")
    public Result categoryPage(CategoryPageQuery pageQuery) {
        IPage<Category> page = categoryService.page(new Page<>(pageQuery.getCurrentPage(), pageQuery.getPageSize()),
                new LambdaQueryWrapper<Category>()
                        .like(StrUtil.isNotBlank(pageQuery.getName()), Category::getName, pageQuery.getName())
        );
        return Result.success(page);
    }

    @ApiOperation(value = "分类列表")
    @GetMapping("list")
    public Result categoryList() {
        List<Category> list = categoryService.list(new LambdaQueryWrapper<Category>()
                .select(Category::getId, Category::getName));
        return Result.success(list);
    }

    @ApiOperation(value = "分类详情")
    @GetMapping("detail/{id}")
    public Result categoryDetail(@PathVariable Long id) {
        Category category = categoryService.getById(id);
        return Result.success(category);
    }

    @Operation(description = "更新分类",businessType = BusinessType.UPDATE)
    @CacheEvict(cacheNames = "category",key = "'list'")
    @ApiOperation(value = "更新分类")
    @PutMapping("update")
    public Result updateCategory(@RequestBody Category category) {
        boolean result = categoryService.updateById(category);
        return Result.judge(result);
    }

    @Operation(description = "添加分类",businessType = BusinessType.INSERT)
    @CacheEvict(cacheNames = "category",key = "'list'")
    @ApiOperation(value = "添加分类")
    @PostMapping("add")
    public Result addCategory(@RequestBody Category category) {
        category.setCreateUserId(JwtUtils.getUserId());
        boolean result = categoryService.save(category);
        return Result.judge(result);
    }

    @Operation(description = "删除分类",businessType = BusinessType.DELETE)
    @CacheEvict(cacheNames = "category",key = "'list'")
    @ApiOperation(value = "删除分类")
    @DeleteMapping("delete/{ids}")
    public Result deleteCategory(@PathVariable String ids) {
        boolean result = categoryService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.judge(result);
    }
}

